package hflyeer.gitee.com.controller;

import hflyeer.gitee.com.entity.Department;
import hflyeer.gitee.com.service.DepartmentService;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("departmentController")
public class DepartmentController {

    @Resource(name = "departmentService")
    private DepartmentService departmentService;

    /**
     * /department/list.do
     * 显示部门列表
     *
     * @param request
     * @param response
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> departments = departmentService.find();
        request.setAttribute("LIST",departments);
        request.getRequestDispatcher("../department_list.jsp").forward(request,response);
    }

    /**
     * /department/toAdd.do
     * 跳转到部门添加界面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect(request.getContextPath()+"/department_add.jsp");
    }

    /**
     * /department/add.do
     * 添加部门
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String location = request.getParameter("location");
        Department department = new Department();
        department.setName(name);
        department.setLocation(location);
        departmentService.add(department);
        response.sendRedirect(request.getContextPath()+"/department/list.do");
    }

    /**
     * /department/toEdit.do
     * 跳转到部门修改界面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Department departmentServiceOne = departmentService.findOne(id);
        request.getSession().setAttribute("Department",departmentServiceOne);
        response.sendRedirect(request.getContextPath()+"/department_edit.jsp");
    }

    /**
     * /department/edit.do
     * 修改部门
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("Department");
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String location = request.getParameter("location");
        Department department = new Department();
        department.setName(name);
        department.setLocation(location);
        department.setId(id);
        departmentService.edit(department);
        response.sendRedirect(request.getContextPath()+"/department/list.do");
    }

    /**
     * /department/remove.do
     * 删除部门
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        departmentService.remove(id);
        response.sendRedirect(request.getContextPath()+"/department/list.do");
    }
}
